window.onload = function () {
    // dom获取
    let username = document.querySelector('[name="username"]')
    let password = document.querySelector('[name="password"]')
    let form = document.querySelector('form')



    // 登录
    form.onsubmit = () => {
        //正则
        let usernameReg = /^\w{4,10}$/
        let passwordReg = /^\d{6}$/

        // 正则验证
        if (!usernameReg.test(username.value)) {
            popup.msg('用户名为4到10位，允许使用数字，字母，下划线！')
            return false
        }
        if (!passwordReg.test(password.value)) {
            popup.msg('密码是六位数字！')
            return false
        }


        promiseRequest({
            method: 'post',
            url: '/front/api/user/login',
            data: {
                username: username.value,
                password: password.value
            }
        }).then(res => {
            if (res.error_code === 200) {
                // 存cookie
                setCookie('username', username.value, 7200)
                popup.msg(res.msg, () => {
                    location.href = '/index.html'
                })
            } else {
                popup(res.msg)
            }
        })
        return false
    }

}